import re
import time

import requests
from lxml import html, etree

url = 'https://tp.9377s.com/1578399/'
res = requests.get(url)
res = res.text
html_tree = etree.HTML(res)
datas = html_tree.xpath(
    "//div[@class='wrap clearfix']/div[@class='game-tj fr']/div[@class='game-tjlist']/ul[@class='clearfix']/li")
# i = 1
# for data in datas:
#     # print(data)
#     img = data.xpath('./a/img')[0]
#     img_src = img.attrib['src']
#     img_res = requests.get(img_src)
#     if str(img_src[-3:]) == 'jpg':
#         with open(f'../datas/网页游戏gif/{i}.jpg', 'wb') as f:
#             f.write(img_res.content)
#         i += 1
#     else:
#         with open(f'../datas/网页游戏gif/{i}.gif', 'wb') as f:
#             f.write(img_res.content)
#         i += 1
for data in datas:
    name = data.xpath('./dl/dt')[0]
    # print(name.text)
    img = data.xpath('./a/img')[0]
    img_src = img.attrib['src']
    img_res = requests.get(img_src)
    if str(img_src[-3:]) == 'jpg':
        with open(f'../datas/网页游戏gif/{name.text}.jpg', 'wb') as f:
            f.write(img_res.content)
    else:
        with open(f'../datas/网页游戏gif/{name.text}.gif', 'wb') as f:
            f.write(img_res.content)
    print(f'{name.text}爬取完毕')
    time.sleep(2)
